package com.hzit.hzb.core.controller.api;


import com.hzit.common.result.R;
import com.hzit.hzb.base.util.JwtUtils;
import com.hzit.hzb.core.hfb.RequestHelper;
import com.hzit.hzb.core.pojo.vo.UserBindVO;
import com.hzit.hzb.core.service.UserBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author hbxfwf
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/userBind")
@Api(tags = "用户绑定接口")
@Slf4j
public class UserBindController {

	@Autowired
	private UserBindService userBindService;
	/**
	 * 用户绑定
	 * @param vo
	 * @param token
	 * @return
	 */
	@PostMapping("/auth/bind")
	@ApiOperation("用户绑定")
	public R userBind(@RequestBody
					  @ApiParam(value="用户绑定基本信息",required = true)
			          UserBindVO vo,
	                  @RequestHeader("token")
			          @ApiParam(value = "前端传入的请求头token",required = true)
			          String token){
		// 1. 根据token得到用户id
		Long userId = JwtUtils.getUserId(token);
		// 2. 进行用户绑定
		String formStr = userBindService.userBind(vo,userId);
		// 3. 将用户绑定的表单返回给前端
		return R.ok().data("formStr",formStr);
	}

	/**
	 * 异步回调，由汇付宝平台调用
	 * @param map
	 * @return
	 */
	@PostMapping("/notify")
	public String notify(HttpServletRequest request){
		Map<String, String[]> map = request.getParameterMap();
		Map<String, Object> paramMap = RequestHelper.switchMap(map);
		if (!RequestHelper.isSignEquals(paramMap)) {
			log.error("验签失败！");
			return "fail";
		}
		// 定义我们自己的业务逻辑
		userBindService.hzbNotify(paramMap);
		return "success";
	}
}

